import axios from 'axios';
import {getToken} from './auth';
import Qs from 'qs';

//请求实例
const instance = axios.create({
  baseURL:'http://127.0.0.1/api',  //公网地址
  timeout:5000                  //请求超时时间
})

//全局请求拦截
instance.interceptors.request.use(
  function (config){
    config.headers["token"] = getToken();
    return config;
  },
  function (error){
    return Promise.reject(error);
  }
)

//get请求
export function get(url,params){
  return instance.get(url,{
    params
  })
}

//post请求
export function post(url,data){
  // const token = localStorage.getItem('token');
  const token = '暂无token';
  data =  Qs.stringify(data);
  return  axios({
    headers:{
      'Content-Type':'application/x-www-form-urlencoded',
      'token':token
    },
    url:'http://47.96.146.127/api' + url,
    method:'post',
    data
  })
}

//put请求
export function put(url,data){
  return instance.put(url,data)
}

//del请求
export function del(url){
  return instance.delete(url)
}
